package com.ngkj.zmfptback.sys.initData.init;

import com.ngkj.zmfptback.sys.initData.bean.SysArea;
import com.ngkj.zmfptback.sys.initData.servicie.read.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author ChenHN
 * @since 2021/10/27 creation
 **/
@Component
public class AreaInit implements ApplicationRunner {

    private static Logger LOGGER = LoggerFactory.getLogger(AreaInit.class);

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private AreaService areaService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        LOGGER.info("======================== 开始加载区域 =================");
        long l = System.currentTimeMillis();
        Map<String, List<SysArea>> allAreas = areaService.getAllAreas();
        allAreas.forEach((k,v)->{
            String cacheKeyName = "appAreaCache."+k;
            redisTemplate.opsForValue().set(cacheKeyName,v);
        });
        Map<String, List<SysArea>> groupFirst = areaService.getGroupFirst();
        String cacheKeyName = "areaFirstGroup";
        redisTemplate.opsForValue().set(cacheKeyName,groupFirst);
        long l1 = System.currentTimeMillis();
        LOGGER.info("======================== 区域加载完成 =================");

    }
}
